package com.mnemonic.views;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.mnemonic.level.Level;
import com.mnemonic.lifecycle.Lifecycles;
import com.mnemonic.storage.impl.LevelStorageService;

/**
 * 难度页设置
 * 
 * @author lifeng
 * @date 2021/05/12
 */
public class LevelPanel extends AbstractPanel {

    private static final long serialVersionUID = 1L;

    private JPanel opsPanel;
    private DefaultTableModel model = null;
    private LevelStorageService levelStorageService;
    private List<Level> levels;

    public LevelPanel() {
        this.setLayout(new BorderLayout(0, 0));
        JScrollPane jsp = new JScrollPane();
        this.add(jsp, BorderLayout.CENTER);
        this.opsPanel = new JPanel();
        this.opsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        this.opsPanel.add(new JButton("添加"));
        this.add(this.opsPanel, BorderLayout.SOUTH);
        String[] columnName = {"ID", "名称", "行数", "列数", "雷数", "每回合可操作的次数", "每回合的秒数"};
        this.model = new DefaultTableModel(columnName, 0);
        JTable table = new JTable(this.model);
        jsp.setViewportView(table);
        jsp.validate();
        jsp.repaint();
    }

    @Override
    public void init() {
        if (!(this.state == null || this.state == Lifecycles.Start)) {
            return;
        }
        super.init();
        this.levelStorageService = new LevelStorageService();
        this.levels = this.levelStorageService.list();
        for (Level level : this.levels) {
            Object[] rowData = new Object[7];
            rowData[0] = level.getId();
            rowData[1] = level.getName();
            rowData[2] = level.getRow();
            rowData[3] = level.getCol();
            rowData[4] = level.getMine();
            rowData[5] = level.getBoutOps();
            rowData[6] = level.getBoutTimeOut();
            this.model.addRow(rowData);
        }
    }
}